<?php

namespace App\Http\Controllers\Api\Main;

use App\Http\Controllers\ApiController;
use App\Models\Coupon;
use App\Models\CouponRule;
use Illuminate\Http\Request;


class CouponController extends ApiController
{
    /**
     * 系统发放的优惠券列表
     */
    public function sys_coupon_list(Coupon $couponModel)
    {
        return self::response($couponModel->api_sys_coupon_list(),self::SUC_CODE,'获取数据成功');
    }

    /**
     * 领取优惠券
     */
    public function receive_coupon(Request $request,Coupon $couponModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required',
            'coupon_id' => 'required',
        ]);
        if($validator -> fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if(!$couponModel -> api_receive_coupon($request->input('user_id'),$request->input('coupon_id'))){
            return self::response(null,self::ERROR_CODE,Coupon::$error);
        }

        return self::response(null,self::SUC_CODE,'优惠券领取成功');

    }

    /**用户已领取的优惠券列表
     * @param Request $request
     * @param Coupon $couponModel
     * @return mixed
     * @throws \ErrorException
     */
    public function user_coupon(Request $request,Coupon $couponModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required',
        ]);
        if($validator -> fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }
        if(!$list = $couponModel -> api_user_coupon($request->input('user_id'))){
            return self::response(null,self::ERROR_CODE,Coupon::$error);
        }

        return self::response($list,self::SUC_CODE,'获取数据成功');
    }

}
